import urllib3

http = urllib3.PoolManager()

import ssl
ssl._create_default_https_context = ssl._create_unverified_context

import re

import os

q_url = 'https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1635134216589_R&pv=&ic=&nc=1&z=&hd=&latest=&copyright=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&dyTabStr=MCwzLDIsNSw4LDEsNCw2LDcsOQ%3D%3D&ie=utf-8&sid=&word=%E5%8D%83%E4%B8%8E%E5%8D%83%E5%AF%BB%E5%A3%81%E7%BA%B8&f=3&oq=qianyu&rsp=1'

headerCon = {  #网页的请求header
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Cookie":"BIDUPSID=93791DB223BA14333A221627719C3CA6; PSTM=1635132674; BAIDUID=93791DB223BA14333F578A47CED6CE4A:FG=1; delPer=0; PSINO=2; H_PS_PSSID=34837_34447_34067_31660_34584_34505_34830_34812_26350_34828; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BA_HECTOR=8h0l8gal808hah24tn1gnc9am0r; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; __yjs_duid=1_07b7d576bbdb6beb597312fbbfd8f3721635132759405; indexPageSugList=%5B%22%E5%8D%83%E4%B8%8E%E5%8D%83%E5%AF%BB%E5%A3%81%E7%BA%B8%22%5D; cleanHistoryStatus=0; H_WISE_SIDS=110085_178384_179348_181589_182530_183328_185517_185649_185925_186318_186635_186665_187292_187356_187450_187669_187929_188331_188467_188615_188846_188870_188888_189714_189731_189755_190034_190507; BDRCVFR[5GQZCjFg8mf]=mk3SLVN4HKm; IMG_WH=1440_789; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; ab_sr=1.0.1_NWM1ZDYxYjRiNmQzYzVmOGMyNWQ3ZTg1ZjM2Mjg5OGMxMzJjZmEwMDFmYzYyMDNlZmY1MTNlNmNkMGY5ZmFjYmNkMTY3M2ExMDcwYTBiYWI1N2QzYTNmMWY2NjQ0YWI5ZmJlOWJiNjFiMDU5YmEzOWI5NTZlMjE4OTRkM2JmMDI2MWI4YTU1NDA4ZmU3ZDY0MjMxMWEzYzZkODU0Yjg1OQ==",
    "Host": "image.baidu.com",
    "Pragma": "no-cache",
    "Referer": "https://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gb18030&word=%C3%C0%C5%AE%CD%BC%C6%AC&fr=ala&ala=1&alatpl=normal&pos=0&dyTabStr=MCwzLDIsNSw4LDEsNCw2LDcsOQ%3D%3D",
    "sec-ch-ua": '"Chromium";v="94", "Google Chrome";v="94", ";Not A Brand";v="99"',
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "macOS",
    "Sec-Fetch-Dest": "document",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "same-origin",
    "Sec-Fetch-User": "?1",
    "Upgrade-Insecure-Requests":"1",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36"
}

response = http.request('GET', url=q_url,headers=headerCon) #获取到网页内容

res_data = response.data.decode()#字节变成字符串

img_urls = re.findall('"thumbURL":"(.*?)"',res_data)  #获取图片地址

for index,i in enumerate(img_urls):
    if 'ns-strategy.cdn.bcebos.com' in i:
        continue
    img_header = {  #图片的请求header
        "accept": "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
        "accept-encoding": "gzip, deflate, br",
        "accept-language": "zh-CN,zh;q=0.9",
        "cache-control": "no-cache",
        "cookie": "BIDUPSID=93791DB223BA14333A221627719C3CA6; PSTM=1635132674; BAIDUID=93791DB223BA14333F578A47CED6CE4A:FG=1; delPer=0; PSINO=2; H_PS_PSSID=34837_34447_34067_31660_34584_34505_34830_34812_26350_34828; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BA_HECTOR=8h0l8gal808hah24tn1gnc9am0r; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; __yjs_duid=1_07b7d576bbdb6beb597312fbbfd8f3721635132759405; H_WISE_SIDS=110085_178384_179348_181589_182530_183328_185517_185649_185925_186318_186635_186665_187292_187356_187450_187669_187929_188331_188467_188615_188846_188870_188888_189714_189731_189755_190034_190507; BDRCVFR[5GQZCjFg8mf]=mk3SLVN4HKm; IMG_WH=1440_789; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; ab_sr=1.0.1_NWM1ZDYxYjRiNmQzYzVmOGMyNWQ3ZTg1ZjM2Mjg5OGMxMzJjZmEwMDFmYzYyMDNlZmY1MTNlNmNkMGY5ZmFjYmNkMTY3M2ExMDcwYTBiYWI1N2QzYTNmMWY2NjQ0YWI5ZmJlOWJiNjFiMDU5YmEzOWI5NTZlMjE4OTRkM2JmMDI2MWI4YTU1NDA4ZmU3ZDY0MjMxMWEzYzZkODU0Yjg1OQ==; userFrom=null",
        "pragma": "no-cache",
        "referer": "https://image.baidu.com/",
        "sec-ch-ua": '"Chromium";v="94", "Google Chrome";v="94", ";Not A Brand";v="99"',
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "macOS",
        "sec-fetch-dest": "image",
        "sec-fetch-mode": "no-cors",
        "sec-fetch-site": "same-site",
        "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36"
    }

    img_req = http.request('GET',url=i,headers=img_header)
    req_data = img_req.data

    if not os.path.exists('qyqx'):
        os.mkdir('qyqx')
    file_name = 'qyqx/'+str(index)+'.jpg'

    with open(file_name,'wb') as f:
        f.write(req_data)